package com.zpark.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

/**
 * @PackageName: com.zpark.config
 * @ClassName: JdbcConfiguration
 * @Description:
 * @author: RZS
 * @date: 2023/3/28  17:04
 */
@Configuration
@PropertySource("classpath:jdbc.properties")
public class JdbcConfiguration {

    @Value("${driver}")
    private String driver;
    @Value("${url}")
    private String url;
    @Value("${uname}")
    private String uname;
    @Value("${pwd}")
    private String pwd;

    @Bean
    public QueryRunner getQueryRunner(DataSource source){
        return new QueryRunner(source);
    }

    @Bean
    public DataSource getDataSource(){
        ComboPooledDataSource source = new ComboPooledDataSource();
        try {
             source.setDriverClass(driver);
             source.setJdbcUrl(url);
             source.setUser(uname);
             source.setPassword(pwd);
        } catch(Exception e) {
            e.printStackTrace();
        }

        return source;
    }
}
